﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.BLL
{
    class Admin
    {

        //<==========查询操作===========>
        //按ID来查询
        public static model.Admin getAdmin(String Aid)
        {

            model.Admin a = new model.Admin();
            String strSql = "select * from T_admin where Aid = @Aid";

            DataTable dt = SQLHelper.ExecuteTable(strSql, new SqlParameter("@Aid", Aid));

            if (dt.Rows.Count <= 0)
            {
                return a;

            }
            else
            {

                a.Aid = dt.Rows[0][0].ToString().Trim();
                a.Aname = dt.Rows[0][1].ToString().Trim();
                a.Pwd = dt.Rows[0][2].ToString().Trim();

                return a;

            }

        }

        //查询所有管理员
        public static List<model.Admin> getAllAdmin()
        {
            List<model.Admin> admins = new List<model.Admin>();
            String strSql = "select * from T_admin";

            DataTable dt = SQLHelper.ExecuteTable(strSql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                model.Admin a = new model.Admin();
                a.Aid = dt.Rows[0][0].ToString().Trim();
                a.Aname = dt.Rows[0][1].ToString().Trim();
                a.Pwd = dt.Rows[0][2].ToString().Trim();

                admins.Add(a);

            }

            return admins;



        }

        //<==========更新操作===========>
        //更新用户信息
        public static bool updateAdmin(model.Admin a)
        {

            int i = 0;

            String strSql = "update T_admin set Aname = @Aname, Pwd = @Pwd where Aid = @Aid";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Aname", a.Aname),
                new SqlParameter("@Pwd", a.Pwd),
                new SqlParameter("@Aid", a.Aid));

            return i > 0 ? true : false;
        }

        //<==========增加操作===========>
        //增加一个用户
        public static bool addAdmin(model.Admin a)
        {

            int i = 0;
            String strSql = "insert into T_admin values(@Aid, @Aname, @Pwd)";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Uid", a.Aid),
                new SqlParameter("@Uname", a.Aname),
                new SqlParameter("@Pwd", a.Pwd));

            return i > 0 ? true : false;

        }

        //<==========删除操作===========>
        public static bool delAdmin(String Aid)
        {

            int i = 0;

            String strSql = "delete T_admin where Aid = @Aid";

            i = SQLHelper.ExecuteNonQuery(strSql, new SqlParameter("@Aid", Aid));

            return i > 0 ? true : false;

        }

        //<++++++++++验证操作++++++++++>
        public static bool isAdmin(String Aname, String Pwd)
        {

            String sqlStr = "select * from T_admin where Aname = @Aname and Pwd = @Pwd";

            DataTable dt = SQLHelper.ExecuteTable(sqlStr,
                new SqlParameter("@Aname", Aname),
                new SqlParameter("@Pwd", Pwd));



            return dt.Rows.Count > 0 ? true : false;
        }


    }
}
